﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;


namespace WakeTheServer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Dynamic_Button_1.Content = WakeTheServer.Properties.Settings.Default.Dynamic_Button_1_Content;
            this.Dynamic_Button_2.Content = WakeTheServer.Properties.Settings.Default.Dynamic_Button_2_Content;
            this.Dynamic_Button_3.Content = WakeTheServer.Properties.Settings.Default.Dynamic_Button_3_Content;
            this.Dynamic_Button_4.Content = WakeTheServer.Properties.Settings.Default.Dynamic_Button_4_Content;
            this.Dynamic_Button_5.Content = WakeTheServer.Properties.Settings.Default.Dynamic_Button_5_Content;
            this.Dynamic_Button_6.Content = WakeTheServer.Properties.Settings.Default.Dynamic_Button_6_Content;
            this.Dynamic_Button_7.Content = WakeTheServer.Properties.Settings.Default.Dynamic_Button_7_Content;
            this.Dynamic_Button_8.Content = WakeTheServer.Properties.Settings.Default.Dynamic_Button_8_Content;
        }

        public static void WakeUp(MACAddress mac)
        {
            byte[] packet = new byte[17 * 6];

            for (int i = 0; i < 6; i++)
                packet[i] = 0xff;

            for (int i = 1; i <= 16; i++)
                for (int j = 0; j < 6; j++)
                    packet[i * 6 + j] = mac[j];

            UdpClient client = new UdpClient();
            client.Connect(IPAddress.Broadcast, 0);
            client.Send(packet, packet.Length);
        }
        private void MenuItem_Click_Close(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void MenuItem_Click_NewPC(object sender, RoutedEventArgs e)
        {
            var newWindow = new SetUpWindow();
            newWindow.Show();
        }

        private void Dynamic_Button_1_Click(object sender, RoutedEventArgs e)
        {
            
            byte Octet_1 = (byte)WakeTheServer.Properties.Settings.Default.Dynamic_Button_1_Octet_1;
            byte Octet_2 = (byte)WakeTheServer.Properties.Settings.Default.Dynamic_Button_1_Octet_2;
            byte Octet_3 = (byte)WakeTheServer.Properties.Settings.Default.Dynamic_Button_1_Octet_3;
            byte Octet_4 = (byte)WakeTheServer.Properties.Settings.Default.Dynamic_Button_1_Octet_4;
            byte Octet_5 = (byte)WakeTheServer.Properties.Settings.Default.Dynamic_Button_1_Octet_5;
            byte Octet_6 = (byte)WakeTheServer.Properties.Settings.Default.Dynamic_Button_1_Octet_6;

            //WakeUp(new MACAddress(new byte[] { 0x00, 0x24, 0x8C, 0xA3, 0x15, 0xAF }));

            WakeUp(new MACAddress(new byte[] { Octet_1, Octet_2, Octet_3, Octet_4, Octet_5, Octet_6 }));
        }

        private void Dynamic_Button_2_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Dynamic_Button_3_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Dynamic_Button_4_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Dynamic_Button_5_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Dynamic_Button_6_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Dynamic_Button_7_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Dynamic_Button_8_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Start_Media_Center_Button_Click(object sender, RoutedEventArgs e)
        {
            Process ExternalProcess = new Process();
            ExternalProcess.StartInfo.FileName = "C:\\Windows\\ehome\\ehshell.exe";
            ExternalProcess.Start();
        }

        
    }
}
